let ws; function connect() { if ("WebSocket" in window) { ws = new WebSocket("ws://...."); // not HTTP // register event listeners on the web socket object ws.onopen = function() { // 'open' event listener; when the channel is open .... = 'Connected'; }; ws.onmessage = function (eobj) { // when a message came from the server .... = eobj.data; }; ws.onclose = function() { // when the channel is closed from the server .... = 'Closed'; }; ws.onerror = function() { // when there is an error .... }; } } function echo() { ws.send(....); // send a message to the server } function end() { ws.close(); // close the channel }
.send()
, .close()
if ("WebSocket" in window)
???WebSocket()
?ws
, not http
.
ws://198.162.21.132:8080/~mlee/comp4620/Software/TRUWSJS/rev.4.fork/test_ws_echo_server.wsjs
Before you test this Trial, make sure you are using http
or http://198.162.21.132:8080
for this web page so that
this Trial can use http.
// Example of standalone WebSocket echo server with the port number 8888 const WebSocketServer = require("ws").Server; const ws_server = new WebSocketServer({port: 8888}); // Use your port number ws_server.on("connection", function(ws_client, request) { // ws_client is an WebSocket object, i.e., a socket for a client. // what if there is a connection request from another clent? // Event listeners on the client web socket object ws_client.on("message", function(msg) { // msg is a Blob object. msg = String(msg); ws_client.send(msg); if (msg == "end") // 'end' is an application level message. ws_client.close(); }); ws_client.on("close", function(code) { console.log(code); ws_client.close(); }); ws_client.on("error", function(err) { console.log(err); ws_client.close(); }); });
// WebSocket server integrated with a web serverd that uses the port number 8888 // HTTP server: const HTTP_PORT_NO = 8888; // Use your port number const http = require("http"); const http_server = http.createServer(function(request, response) { ... }); http_server.listen(HTTP_PORT_NO); // WebSocket server: const WebSocketServer = require("ws").Server; // WebSocket server integrated with a web server const ws_server = new WebSocketServer({server: http_server}); // different from the standalone version ws_server.on("???", function(ws_client, request) { // ws_client is an WebSocket object, i.e., a socket for a client. // what if there is a connection request from another clent? ws_client.on("???", function(msg) { // msg is a Blob object. msg = String(msg); if (msg == "end" || msg == "close" || msg == "quit") { ws_client.???(); } else { ws_client.???(msg); // echoing } }); ws_client.on("???", function(msg) { console.log('closed'); }); });
ws_echo_server.js
, that uses a standalone server, and run it.
(You many need to install the 'ws' module.)const express = require("express"); const app = express(); const expressWs = require("express-ws"); expressWs(app); app.listen(8888); // It should be done after expressWs(app). Use your port number. // Express service for a route app.get("/", function(req, res, next){ ... }); // WebSocekt service for the "/echos" path app.ws("/echos", function(wsClient, req) { console.log("connected"); wsClient.on("???", function(msg) { // msg is a string. wsClient.???(msg); }); wsClient.???("???", function(msg) { console.log("closed"); }); });
'{command: join, name: ..., password: ...}'
'{command: sign_in, name: ..., password: ...}'
'{command: sign_out, name: ...}'
'{command: okay, for: ...}'
'{command: notokay, for: ...}'
'{command: signed_in, name: ...}'
'{command: signed_out, name: ...}'
'{command: one_to_one, name: ..., to: ..., message: ...}'
'{command: broadcasting, name: ..., message: ...}'
'{command: from, name: ..., message: ...}'
'{command: error, message: ...}'
Client-A (Dave) Server Client-B (John) ----------------Connection--------------------> ws_client object created ---->'{command: sign_in, name: Dave, ???}'----> <----'{command: okay|notokay, for: sign_in}'--- keep 'Dave' and ws_client socket object in an object <----'{command: signed_in, name: John}'-------- <----'{command: ???, name: Tom}'--------------- ----------->'{command: signed_in, name: Dave}'-------------> ---->'{command: one_to_one, name: Dave, to: John, message: Hello}' ----------->'{command: from, name: Dave, message: Hello}'--> ---->'{command: ???, name: ???, to: John, ???: Bye}' ----------->'{command: ???, name: Dave, message: Bye}'-----> ---->'{command: sign_out, name: Dave}'--------> Delete 'Dave' and ws_client socket object from the object ----------->'{command: ???, name: Dave}'-------------------> <---------------Disconnection----------------->
let ws = new WebSocket("ws://198.162.21.132:8888/"); // should be ws: ws.onopen = function() { // Web Socket is connected, send messages using send() ... }; ws.onmessage = function (evt) { let msgfromserver = JSON.parse(evt.data); switch(msgfromserver.command) { case 'signed_in': ... case 'signed_out': ... case 'from': $('#output').append(msgfromserver.name + ': ' + msgfromserver.message + '<br>'); ... case 'error': ... } }; ws.onclose = function() { // websocket is closed. ... }; ws.onerror = function(evt) { ... }; $('#one_to_one').click(function() { let msgtoserver = {}; msgtoserver.command = 'one_to_one'; msgtoserver.name = myname; msgtoserver.to = $('#receiver').text(); msgtoserver.message = $('#message').text(); ws.send(JSON.stringify(msgtoserver)); }); ...
const WebSocketServer = require('ws').Server, ws_server = new WebSocketServer({port: 8888}); ws_server.on('connection', function(ws_client) { // ws_client is an WebSocket object, i.e., a socket for clients. ws_client.on('message', function(msgfromclient) { ws_client.send(msgfromclient); if (msgfromclient == 'end') ws_client.close(); }); ws_client.on('close', function(code) { ws_client.close(); }); ws_client.on('error', function(err) { ws_client.close(); }); });
const WebSocketServer = require('ws').Server, ws_server = new WebSocketServer({port: 8888}); let ws_clients = {}; // {userName:ws_client, ...}, where userNames are unique ws_server.on('connection', function(ws_client) { // ws_client is an WebSocket object, i.e., a socket for clients. ws_client.on('message', function(message) { msgfromclient = JSON.parse(message); switch(msgfromclient.command) { case 'join': ... break; case 'sign_in': ... ws_clients[msgfromclient.name] = ws_client; ... break; case 'sign_out': ... ??? ws_clients[msgfromclient.name]; // delete the corresponding ws_client ... break; case 'one_to_one': // find the ws_client for msgfromclient.to, and send msgfromclient.message to the ws_client ws_clients[???].send(???); break; case 'broadcasting': ... break; ... } }); ws_client.on('close', function(code) { ... // broadcasting it ... // find the socket from ws_clients and delete it }); ws_client.on('error', function(err) { ... // broadcasting it ... // find the socket from ws_clients and delete it }); });